package com.zhan.signalling.data.listener;

import java.util.Collection;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.listener.DataListener;
import com.google.common.collect.Lists;
import com.zhan.signalling.check.AccoutPermissionCheck;
import com.zhan.signalling.commons.Constants;
import com.zhan.signalling.commons.SocketEvent;
import com.zhan.signalling.message.Message;
import com.zhan.signalling.message.Result;

/**
 * 获取房间人员信息
 * @author knight
 *
 */
public class GetGroupMembersListener implements DataListener<Message> {

	@Override
	public void onData(SocketIOClient client, Message data, AckRequest ackSender) throws Exception {
		
		// 校验权限
		AccoutPermissionCheck.check(client, SocketEvent.GET_GROUP_MEMBERS_EVENT);
		
		String groupId = client.get(Constants.GROUP_KEY);
		Collection<SocketIOClient> accouts = client.getNamespace().getRoomOperations(groupId).getClients();
		final List<JSONObject> users = Lists.newArrayList();
		accouts.forEach(x->{
			JSONObject user = x.get(Constants.USER_KEY);
			users.add(user);
		});
		client.sendEvent(SocketEvent.GET_GROUP_MEMBERS_EVENT, 
				new Result(users, Constants.SUCCESS));
	}

}
